package com.group.project.hrms.service.impl;

import com.group.project.hrms.entity.DingClockRecord;
import com.group.project.hrms.mapper.DingClockRecordMapper;
import com.group.project.hrms.service.IDingClockRecordService;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service
public class DingClockRecordService implements IDingClockRecordService {

    @Autowired
    private DingClockRecordMapper dingClockRecordMapper;

    @Autowired
    private SqlSessionFactory sqlSessionFactory;

    @Override
    public int insertDingClockRecord(DingClockRecord dingClockRecord) {
        return dingClockRecordMapper.insert(dingClockRecord);
    }

    @Override
    public int insertDingClockRecords(List<DingClockRecord> dingClockRecords) {
        return insertBatch(dingClockRecords);
    }

    @Override
    public int deleteDingClockRecord(DingClockRecord dingClockRecord) {
        return dingClockRecordMapper.deleteByPrimaryKey(dingClockRecord);
    }

    @Override
    public int deleteDingClockRecordByMonth(String month) {
        Example example = new Example(DingClockRecord.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.orLike("userchecktime", "%" + month + "%");
        return dingClockRecordMapper.deleteByExample(example);
    }

    @Override
    public int updateDingClockRecord(DingClockRecord dingClockRecord) {
        return 0;
    }

    @Override
    public DingClockRecord selectOneDingClockRecord(DingClockRecord dingClockRecord) {
        return null;
    }

    @Override
    public List<DingClockRecord> selectDingClockRecordByDate(String date) {
        Example example = new Example(DingClockRecord.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.orLike("userchecktime", "%" + date + "%");
        return dingClockRecordMapper.selectByExample(example);
    }

    @Override
    public List<DingClockRecord> selectDingClockRecordByMonthAndUserID(String month, String userID) {
        Example example = new Example(DingClockRecord.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.orEqualTo("userid", userID);
        criteria.orLike("userchecktime", "%" + month + "%");
        return dingClockRecordMapper.selectByExample(example);
    }

    @Override
    public List<DingClockRecord> selectDingClockRecordByRangeDate(String startDate, String endDate) {
        Example example = new Example(DingClockRecord.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andBetween("userchecktime", startDate, endDate);
        return dingClockRecordMapper.selectByExample(example);
    }

    @Override
    public List<DingClockRecord> selectAllDingClockRecord() {
        return dingClockRecordMapper.selectAll();
    }


    private int insertBatch(List<DingClockRecord> itemList){
        int count = 0;
        SqlSession session = sqlSessionFactory.openSession(ExecutorType.BATCH,false);
        DingClockRecordMapper batchMapper = session.getMapper(DingClockRecordMapper.class);
        for (int i = 0; i < itemList.size(); i++) {
            batchMapper.insert(itemList.get(i));
            count = i + 1;
        }
        session.commit();
        session.clearCache();
        return count;
    }

}
